package com.iwhalecloud.citybrain.flow.platform.manager.application.service.impl.task;

import com.alibaba.cola.dto.Response;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.iwhalecloud.citybrain.flow.platform.manager.application.common.enums.ProcessInstanceActionEnum;
import com.iwhalecloud.citybrain.flow.platform.manager.application.common.enums.ProcessInstanceStatusEnum;
import com.iwhalecloud.citybrain.flow.platform.manager.application.common.enums.TaskLinkStatusEnum;
import com.iwhalecloud.citybrain.flow.platform.manager.application.common.enums.TaskNodeTypeEnum;
import com.iwhalecloud.citybrain.flow.platform.manager.application.service.UserService;
import com.iwhalecloud.citybrain.flow.platform.manager.application.service.UserTaskManageService;
import com.iwhalecloud.citybrain.flow.platform.manager.application.utils.StringUtils;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.FlowProcessInstanceTraceVo;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.userentity.UserInfo;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.repository.FlowProcessInstanceTraceRepository;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
　　* @description: 同步流程或任务信息
　　* @author shenrm
　　* @date 2021/12/29 20:12
　　*/
@Slf4j
@Service
public class SyncProcessTaskProcessor {

    @Autowired
    private UserTaskManageService userTaskManageService;

    @Autowired
    private UserService userService;

    @Autowired
    private FlowProcessInstanceTraceRepository flowProcessInstanceTraceRepository;

    @Autowired
    private EventExceptionHandler eventExceptionHandler;

    /**
     * 根据不同的场景更新同步第三方的流程实例或任务实例状态数据
     * @param proInstanceIds
     * @param taskIds
     * @param status
     * @param linkStatus
     */
    public void syncDataByScene(List<String> proInstanceIds, List<String> taskIds, String status, String linkStatus) {

    }

    /**
     * 根据不同的场景更新同步第三方的流程实例或任务实例状态数据
     * @param proInstanceIds
     * @param taskIds
     * @param status
     * @param linkStatus
     * @param multiTask 是否会签
     */
    public void syncDataByScene(List<String> proInstanceIds, List<String> taskIds, String status, String linkStatus,Boolean multiTask) {

    }


    /**
     * 按照场景处理人更新成新人
     * @param taskIds
     * @param userId
     * @param isTransfer
     */
    public void syncDataBySceneAndUser(List<String> taskIds, String userId, Boolean isTransfer) {
        log.info("SyncProcessTaskProcessor流程组件更新待办组件处理人接口入参1taskIds：{},入参2userId：{}，入参3isTransfer：{}",taskIds,userId,isTransfer);

    }


    /**
     * 删除任务
     * @param taskIds
     */
    public void deleteTask(List<String> taskIds) {
        if(CollectionUtils.isEmpty(taskIds)) {
            return;
        }
    }


    /**
     * 根据行为或动作同步并更新流程实例或任务实例的状态
     * @param processInstanceIds
     * @param taskIds
     * @param action
     */
    public void syncUpdDbInfoByProcessOrTask(List<String> processInstanceIds, List<String> taskIds, String action) {
        String status = null;
        if(Objects.equals(action, ProcessInstanceActionEnum.PAUSE.getCode())) { // 暂停(挂起)
            status = ProcessInstanceStatusEnum.PAUSE.getCode();
        } else if(Objects.equals(action, ProcessInstanceActionEnum.RECALL.getCode())) { // 撤单
            status = ProcessInstanceStatusEnum.RECALL.getCode();
        } else if(Objects.equals(action, ProcessInstanceActionEnum.END.getCode())) { // 终止
            status = ProcessInstanceStatusEnum.END.getCode();
        } else if(Objects.equals(action, ProcessInstanceActionEnum.UNPAUSE.getCode())) { // 激活(解挂)
            if(CollectionUtils.isNotEmpty(processInstanceIds)) {
                List<FlowProcessInstanceTraceVo> voList = flowProcessInstanceTraceRepository.getProcessInstanceTracesWithoutUserCheck(processInstanceIds);
                if (!org.springframework.util.CollectionUtils.isEmpty(voList)){
                    Map<Integer, String> flagToStatus = new HashMap<Integer, String>();
                    flagToStatus.put(0, ProcessInstanceStatusEnum.NORMAL.getCode());
                    flagToStatus.put(1, ProcessInstanceStatusEnum.WARNING.getCode());
                    flagToStatus.put(2, ProcessInstanceStatusEnum.TIMEOUT.getCode());
                    if(Objects.equals(action, ProcessInstanceActionEnum.UNPAUSE.getCode())) {
                        status = flagToStatus.get(voList.get(0).getFlag());
                    }
                }
            } else {
                status = TaskLinkStatusEnum.TODO.getCode();
            }
        }

        if(CollectionUtils.isNotEmpty(processInstanceIds)) {
            this.syncDataByScene(processInstanceIds, null, status, null);
        }

        if(CollectionUtils.isNotEmpty(taskIds)) {
            this.syncDataByScene(null, taskIds, null, status);
        }
    }

    /**
     * 根据流程实例更新流程实例紧急程度
     *
     * @param processInstanceId
     * @param urgentLevel
     */
    public void syncDataByProcess(String processInstanceId, int urgentLevel) {
        if(StringUtils.isEmpty(processInstanceId)) {
            return;
        }

    }


}
